package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import flash.events.*;
    import flash.geom.*;
    import modules.*;
    import mortal.game.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene.ai.*;
    import mortal.game.scene.events.*;
    import mortal.game.scene.layer.utils.*;
    import mortal.game.scene.map.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.rest.*;
    import mortal.game.view.system.*;
    import mortal.game.view.uiIconBtn.*;
    import mortal.mvc.core.*;

    public class RestDoubleController extends Controller
    {
        private var _restSpecial:RestSpecialModule;
        private var _restBriefIntro:RestBriefIntroModule;
        private var _restSearchPlayer:RestSearchPlayerModule;
        private var _restDiscontinue:RestDiscontinueModule;
        private var _nameInviteArr:Array;
        private var _applyZazenArr:Array;
        private var _restHintIcon:RestHintIcon;
        private var _introX:int;
        private var _introY:int;

        public function RestDoubleController()
        {
            this._nameInviteArr = [];
            this._applyZazenArr = [];
            
        }

        public function get restSpecial() : RestSpecialModule
        {
            if (this._restSpecial == null)
            {
                this._restSpecial = new RestSpecialModule();
            }
            return this._restSpecial;
        }

        public function get restBriefIntro() : RestBriefIntroModule
        {
            if (this._restBriefIntro == null)
            {
                this._restBriefIntro = new RestBriefIntroModule();
            }
            return this._restBriefIntro;
        }

        public function get restSearchPlayer() : RestSearchPlayerModule
        {
            if (this._restSearchPlayer == null)
            {
                this._restSearchPlayer = new RestSearchPlayerModule();
            }
            return this._restSearchPlayer;
        }

        public function get restDiscontinue() : RestDiscontinueModule
        {
            if (this._restDiscontinue == null)
            {
                this._restDiscontinue = new RestDiscontinueModule();
            }
            return this._restDiscontinue;
        }

        public function get restHintIcon() : RestHintIcon
        {
            if (this._restHintIcon == null)
            {
                this._restHintIcon = new RestHintIcon();
                this._restHintIcon.addEventListener(MouseEvent.CLICK, this.onRestHintIconClickHandler);
            }
            return this._restHintIcon;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.Rest_Reiki, this.onRestReikiHandler);
            Dispatcher.addEventListener(EventName.Rest_DoubleRest, this.onRestDoubleRestHandler);
            Dispatcher.addEventListener(EventName.Rest_Profit, this.onRestProfitHandler);
            Dispatcher.addEventListener(EventName.RestIntroOperate, this.onOperateRestIntroHandler);
            Dispatcher.addEventListener(EventName.Rest_Reiki_Begin, this.onBeginRestReikiHandler);
            Dispatcher.addEventListener(EventName.Rest_Reiki_Cancel, this.onCancelRestReikiHandler);
            Dispatcher.addEventListener(EventName.Rest_BeginDoubleRest, this.onBeginDoubleRestHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDoubleZazenInvite, this.onDoubleZazenInviteHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateZazenRewardInfo, this.onBreakOffDoubleRestHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDoubleZazenCanMove, this.onDoubleZazenCanMoveHandler);
            NetDispatcher.addCmdListener(ServerCommand.PlayerAttributeStatusUpdate, this.onAttributeStatusUpdateHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.DoubleRest, this.onDoubleRestHandler);
            
        }

        private function onDoubleRestHandler(event:PlayerEvent) : void
        {
            var _loc_2:* = event.player as UserPlayer;
            if (_loc_2.isDoubleRest)
            {
                this.restBriefIntro.hide();
                this.restSearchPlayer.hide();
            }
            
        }

        private function onRestReikiHandler(event:DataEvent) : void
        {
            this.restSpecial.show();
            
        }

        private function onRestDoubleRestHandler(event:DataEvent) : void
        {
            this._introX = Global.stage.width / 2 - this.restBriefIntro.width;
            this._introY = (Global.stage.height - this.restBriefIntro.height) / 2;
            var _loc_2:* = this._introX + this.restBriefIntro.width;
            var _loc_3:* = (Global.stage.height - this.restSearchPlayer.height) / 2;
            this.restSearchPlayer.show(_loc_2, _loc_3);
            
        }

        private function onRestProfitHandler(event:DataEvent) : void
        {
            
        }

        private function onOperateRestIntroHandler(event:DataEvent) : void
        {
            if (!this.restSearchPlayer.isHide && this.restBriefIntro.isHide)
            {
                this.restBriefIntro.show(this._introX, this._introY);
            }
            else
            {
                this._restBriefIntro.hide();
            }
            
        }

        private function onBeginRestReikiHandler(event:DataEvent) : void
        {
            GameProxy.roleProxy.setNimbusZazen(true);
            
        }

        private function onCancelRestReikiHandler(event:DataEvent) : void
        {
            GameProxy.roleProxy.setNimbusZazen(false);
            
        }

        private function onBeginDoubleRestHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SEntityId;
            GameProxy.roleProxy.applyZazen(_loc_2);
            
        }

        private function onBreakOffDoubleRestHandler(param1:MessageBlock) : void
        {
            if (!this.restBriefIntro.isHide)
            {
                this.restBriefIntro.hide();
            }
            if (!this.restSearchPlayer.isHide)
            {
                this.restSearchPlayer.hide();
            }
            if (!this.restSpecial.isHide)
            {
                this.restSpecial.hide();
            }
            this.restDiscontinue.show();
            var _loc_2:* = param1.messageBase as SZazenReward;
            this._restDiscontinue.update(_loc_2);
            
        }

        private function onDoubleZazenInviteHandler(param1:MessageBlock) : void
        {
            var _loc_3:String = null;
            var _loc_4:int = 0;
            var _loc_2:* = param1.messageBase as SApplyZazen;
            if (!SystemSetter.localSetter.acceptInviteRest)
            {
                _loc_3 = _loc_2.playerInfo.name;
                _loc_4 = 0;
                while (_loc_4 < this._nameInviteArr.length)
                {
                    
                    if (this._nameInviteArr[_loc_4] == _loc_3)
                    {
                        
                    }
                    _loc_4++;
                }
                this._nameInviteArr.push(_loc_3);
                this._applyZazenArr.push(_loc_2);
                UiHintIconView.instance.addHintIcon(this.restHintIcon);
                this.restHintIcon.requestArr = this._applyZazenArr;
            }
            else
            {
                GameProxy.roleProxy.replyZazen(_loc_2.applyId, EZazenResult.EZazenResultCannotRecv);
            }
            
        }

        private function onRestHintIconClickHandler(event:MouseEvent) : void
        {
            var sApplyZazen:SApplyZazen;
            var onCloseHandler:Function;
            var e:* = event;
            onCloseHandler = function (param1:int) : void
            {
                if (param1 == Alert.YES)
                {
                    GameProxy.roleProxy.canDoubleZazen(sApplyZazen.applyId);
                }
                else
                {
                    GameProxy.roleProxy.replyZazen(sApplyZazen.applyId, EZazenResult.EZazenResultReject);
                }
                
            }
            ;
            sApplyZazen = this._applyZazenArr.shift() as SApplyZazen;
            this._nameInviteArr.shift();
            var campColor:* = ColorConfig.instance.getCountryColor(sApplyZazen.playerInfo.camp).color;
            Alert.show("<font color=\'" + campColor + "\'>" + sApplyZazen.playerInfo.name + "</font> 邀请你双修，是否同意？", null, Alert.YES | Alert.NO, null, onCloseHandler);
            if (this._applyZazenArr.length > 0)
            {
                this.restHintIcon.requestArr = this._applyZazenArr;
            }
            else
            {
                EffectManager.glowFilterUnReg(this._restHintIcon);
                this._restHintIcon.removeEventListener(MouseEvent.CLICK, this.onRestHintIconClickHandler);
                UiHintIconView.instance.removeHintIcon(this._restHintIcon);
                this._restHintIcon = null;
            }
            
        }

        private function onDoubleZazenCanMoveHandler(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SApplyZazen;
            this.moveToDoubleZazen(_loc_2);
            
        }

        private function moveToDoubleZazen(param1:SApplyZazen) : void
        {
            var player:UserPlayer;
            var onWalkEndHandler:Function;
            var onGirdWalkEndHandler:Function;
            var endPoint:Point;
            var sApplyZazen:* = param1;
            player = ThingUtil.entityUtil.getEntity(sApplyZazen.toEntityId) as UserPlayer;
            if (player != null)
            {
                if (RolePlayer.instance.isInDoubleRestRange(player))
                {
                    GameProxy.roleProxy.replyZazen(sApplyZazen.applyId, EZazenResult.EZazenResultAgree);
                }
                else
                {
                    onWalkEndHandler = function (event:PlayerEvent) : void
            {
                Game.scene.mouseEnabled = true;
                AIManager.cancelAll();
                GameProxy.roleProxy.replyZazen(sApplyZazen.applyId, EZazenResult.EZazenResultAgree);
                RolePlayer.instance.removeEventListener(PlayerEvent.WALK_END, onWalkEndHandler);
                
            }
            ;
                    onGirdWalkEndHandler = function (event:PlayerEvent) : void
            {
                if (RolePlayer.instance.isInDoubleRestRange(player))
                {
                    Game.scene.mouseEnabled = true;
                    event.stopImmediatePropagation();
                    RolePlayer.instance.stopWalking();
                    RolePlayer.instance.removeEventListener(PlayerEvent.GIRD_WALK_END, onGirdWalkEndHandler);
                }
                
            }
            ;
                    endPoint = GameMapUtil.getTilePoint(player.x, player.y);
                    Game.scene.mouseEnabled = false;
                    RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, onGirdWalkEndHandler);
                    RolePlayer.instance.addEventListener(PlayerEvent.WALK_END, onWalkEndHandler);
                    Game.scene.moveRole(endPoint);
                }
            }
            
        }

        private function onAttributeStatusUpdateHandler(param1:int) : void
        {
            if (param1 == EMoveStatus._EMoveStatusZazen)
            {
                MsgManager.addTipText("进入打坐状态");
            }
            else if (param1 == EMoveStatus._EMoveStatusDoubleZazen)
            {
                MsgManager.addTipText("进入双修状态");
            }
            
        }

    }
}
